<template>
  <div class="articles-add">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <quill-editor
          ref="text"
          v-model="ruleForm.articleBody"
          class="myQuillEditor"
          :options="editorOption"
        />
        <!-- 文章标题，内容，视频 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addArticle('ruleForm')"
            >确 定</el-button
          >
        </span>
      </el-form-item>
      <el-form-item label="视频地址" prop="videoURL">
        <el-input v-model="ruleForm.videoURL"></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="addArticle('ruleForm')"
        >确 定</el-button
      >
    </div>
  </div>
</template>

<script>
import { add } from '@/api/hmmm/articles'
import { deepClone } from '../../utils/index'
// import { cloneDeep } from "lodash";
export default {
  name: 'ArticleAdd',
  props: {
    handle: {
      type: String,
      required: true
    },
    ListForm: {
      type: Object,
      required: false
    }
  },
  created() {
    if (this.handle === 'update') {
      this.ruleForm = deepClone(this.ListForm)
    }
  },
  data() {
    return {
      editorOption: {
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // toggled buttons
            [{ list: 'ordered' }, { list: 'bullet' }, 'blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      },
      ruleForm: {
        title: null, // 文章标题
        articleBody: null, // 文章内容
        videoURL: null // 视频地址
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    addArticle(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          add(this.ruleForm)
            .then(() => {
              this.$emit('initDate', this.ruleForm.title)
              this.$message.success('操作成功！')
              this.ruleForm = {}
            })
            .catch(() => {
              this.$message.error('操作失败！')
            })
          this.$emit('close')
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
